package com.easyfun.healthmagicbox.inputviews;

import java.sql.SQLException;
import java.util.Calendar;

import com.easyfun.healthmagicbox.HMBMainActivity;
import com.easyfun.healthmagicbox.R;
import com.easyfun.healthmagicbox.SubLastProfileActivity;
import com.easyfun.healthmagicbox.ViewPagerActivity;
import com.easyfun.healthmagicbox.base.BaseActivity;
import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.HealthInputDataUploadTask;
import com.easyfun.healthmagicbox.comm.HealthThreadPool;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.HealthInputTypeEnum;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.HealthInputData;
import com.easyfun.healthmagicbox.pojo.PersonProfile;
import com.easyfun.healthmagicbox.sync.BasicCommCallback;
import com.easyfun.healthmagicbox.util.HealthDateUtil;
import com.easyfun.healthmagicbox.widget.DateTimeOnTouchListener;
import com.easyfun.healthmagicbox.widget.SeekBarWithAdjust;
import com.j256.ormlite.dao.Dao;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class FitnessInputActivity extends BaseActivity implements
		DatabaseAccess {
	private String LOG_TAG = FitnessInputActivity.class.toString();
	private SparseArray<String> changedValues = new SparseArray<String>();

	SeekBarWithAdjust seekbarrunkm;
	TextView txtrunkm;
	SeekBarWithAdjust seekbarrunmins;
	TextView txtrunmins;
	EditText inputdatetime;

	SeekBarWithAdjust seekbaryangwoqizuo;
	TextView txtyangwoqizuoge;
	SeekBarWithAdjust seekbaryitixiangshang;
	TextView txtyitixiangshangoge;
	SeekBarWithAdjust seekbarfuwocheng;
	TextView txtfuwochengge;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inputfitness);
		// should after set Content
		addbackButton(this);
		// personal ID
		if (!checkPersonId())
			return;
		seekbarrunkm = (SeekBarWithAdjust) findViewById(R.id.seekbarrunkm);
		txtrunkm = (TextView) findViewById(R.id.txtrunkm);

		seekbarrunmins = (SeekBarWithAdjust) findViewById(R.id.seekbarrunmins);
		txtrunmins = (TextView) findViewById(R.id.txtrunmins);

		seekbaryangwoqizuo = (SeekBarWithAdjust) findViewById(R.id.seekbaryangwoqizuo);
		txtyangwoqizuoge = (TextView) findViewById(R.id.txtyangwoqizuoge);

		seekbaryitixiangshang = (SeekBarWithAdjust) findViewById(R.id.seekbaryitixiangshang);
		txtyitixiangshangoge = (TextView) findViewById(R.id.txtyitixiangshangoge);

		seekbarfuwocheng = (SeekBarWithAdjust) findViewById(R.id.seekbarfuwocheng);
		txtfuwochengge = (TextView) findViewById(R.id.txtfuwochengge);

		inputdatetime = (EditText) findViewById(R.id.datetimeedit);
		inputdatetime.setText(HealthDateUtil.getCurrentDateStr("yyyy-MM-dd HH:mm"));
		inputdatetime.setOnTouchListener(
				new DateTimeOnTouchListener(this, inputdatetime));

		setupView();
		Button butSave = (Button) findViewById(R.id.btn_fitness_save);
		butSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				saveFitnessData();
				Toast.makeText(FitnessInputActivity.this, "保存", Toast.LENGTH_LONG).show();
				
				finish();
				// final Intent intent = new Intent(FitnessInputActivity.this,
				// ViewPagerActivity.class);
				// intent.putExtra(ConstantData.HMBPERSONID, getPersonid());
				// FitnessInputActivity.this.startActivity(intent);
			}

		});

	}

	void setupView() {
		Dao<HealthInputData, Integer> inputDataDao;
		// get our dao
		try {
			inputDataDao = getHelper(this).getInputDataDao();
			Log.i(LOG_TAG, "get last input data.");
		} catch (SQLException e) {
			e.printStackTrace();
			return;
		}
		//

		// -------------------------------------------------------------------------------
		seekbarrunkm.setMax(10);
		seekbarrunkm.setProgress(1);
		txtrunkm.setText(String.valueOf(seekbarrunkm.getProgress())
				+ this.getString(R.string.paobu_kongli));
		seekbarrunkm.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
				HealthInputTypeEnum.RUNKM));

		// -------------------------------------------------------------------------------
		seekbarrunmins.setMax(120);
		seekbarrunmins.setProgress(10);
		txtrunmins.setText(String.valueOf(seekbarrunmins.getProgress())
				+ this.getString(R.string.paobu_fenzhong));

		seekbarrunmins
				.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
						HealthInputTypeEnum.RUNMINS));

		// -------------------------------------------------------------------------------
		seekbaryangwoqizuo.setMax(200);
		seekbaryangwoqizuo.setProgress(100);
		txtyangwoqizuoge.setText(String.valueOf(seekbaryangwoqizuo
				.getProgress()) + this.getString(R.string.yundong_ge));

		seekbaryangwoqizuo
				.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
						HealthInputTypeEnum.YANGWOQIZUO));

		// -------------------------------------------------------------------------------
		seekbaryitixiangshang.setMax(100);
		seekbaryitixiangshang.setProgress(100);
		txtyitixiangshangoge.setText(String.valueOf(seekbaryitixiangshang
				.getProgress()) + this.getString(R.string.yundong_ge));

		seekbaryitixiangshang
				.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
						HealthInputTypeEnum.YINTIXIANGSHANG));

		// -------------------------------------------------------------------------------
		seekbarfuwocheng.setMax(200);
		seekbarfuwocheng.setProgress(100);
		txtfuwochengge.setText(String.valueOf(seekbarfuwocheng.getProgress())
				+ this.getString(R.string.yundong_ge));

		seekbarfuwocheng
				.setOnSeekBarChangeListener(new MyOnSeekBarChangeListener(
						HealthInputTypeEnum.FUWOCHENG));

	}

	void saveFitnessData() {
		Dao<HealthInputData, Integer> inputDataDao;
		// get our dao
		try {
			inputDataDao = getHelper(this).getInputDataDao();
			Log.i(LOG_TAG, "get last input data.");
		} catch (SQLException e) {
			e.printStackTrace();
			return;
		}
		// key is the value type which is enum
		int key = 0;
		for (int i = 0; i < changedValues.size(); i++) {
			key = changedValues.keyAt(i); // get the object by the key.
			String value = changedValues.get(key);
			HealthInputData hid = new HealthInputData();
			hid.setPersonID(m_personid);
			long dt = HealthDateUtil.getDate(inputdatetime.getText().toString(),"yyyy-MM-dd HH:mm").getTime();
			hid.setValueTime(dt);// HealthDateUtil.getCurrentDateStr()
			hid.setValueType(key);
			hid.setValueData(value);

			try {
				inputDataDao.create(hid);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				changedValues.clear();
				return;
			}

		}

		HealthInputDataUploadTask task = new HealthInputDataUploadTask(this,
				m_personid, new BasicCommCallback());
		HealthThreadPool.getInstance().execute(task);

		changedValues.clear();
		// close myself equal to go back.
		finish();

	}

	class MyOnSeekBarChangeListener implements OnSeekBarChangeListener {
		private HealthInputTypeEnum mtype;

		public MyOnSeekBarChangeListener(HealthInputTypeEnum type) {
			mtype = type;
		}


		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub
			// TODO Auto-generated method stub

						changedValues.append(mtype.toInteger(),
								String.valueOf(seekBar.getProgress()));

						switch (mtype) {
						case RUNKM:
							txtrunkm.setText(String.valueOf(seekBar.getProgress())
									+ FitnessInputActivity.this
											.getString(R.string.paobu_kongli));
							break;
						case RUNMINS:
							txtrunmins.setText(String.valueOf(seekBar.getProgress())
									+ FitnessInputActivity.this
											.getString(R.string.paobu_fenzhong));
							break;

						case YANGWOQIZUO:
							txtyangwoqizuoge.setText(String.valueOf(seekBar.getProgress())
									+ FitnessInputActivity.this
											.getString(R.string.yundong_ge));
							break;
						case YINTIXIANGSHANG:
							txtyitixiangshangoge.setText(String.valueOf(seekBar
									.getProgress())
									+ FitnessInputActivity.this
											.getString(R.string.yundong_ge));
							break;
						case FUWOCHENG:
							txtfuwochengge.setText(String.valueOf(seekBar.getProgress())
									+ FitnessInputActivity.this
											.getString(R.string.yundong_ge));
							break;
						default:
							break;
						}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

	}

	@Override
	public DatabaseHelper getHelper(Context context) {
		return DatabaseAccessImplementation.getInstance().getHelper(this);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {

		super.onWindowFocusChanged(hasFocus);
		if (hasFocus) {
			
		} else {
			changedValues.clear();
		}
	}
}
